package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type AssetCheckListAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetCheckListAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetCheckListAddLogic {
	return &AssetCheckListAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetCheckListAdd 创建资产清单
func (l *AssetCheckListAddLogic) AssetCheckListAdd(in *property_service.AssetCheckList) (*property_service.Response, error) {
	// 获取资产清单模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetCheckList
	// 创建一个新的资产清单模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.AssetCheckList{
		CheckID:         in.CheckID,         // 盘点单ID
		AssetID:         in.AssetID,         // 资产ID
		AssetName:       in.AssetName,       // 资产名称
		CardNumber:      in.CardNumber,      // 卡片编码
		AssetCode:       in.AssetCode,       // 资产编码
		SpecModel:       in.SpecModel,       // 规格型号
		BookAmount:      in.BookAmount,      // 账面金额
		BookQuantity:    in.BookQuantity,    // 账面数量
		ActualAmount:    in.ActualAmount,    // 盘点金额
		ActualQuantity:  in.ActualQuantity,  // 盘点数量
		SurplusAmount:   in.SurplusAmount,   // 盈亏金额
		SurplusDesc:     in.SurplusDesc,     // 盈亏说明
		StorageLocation: in.StorageLocation, // 存放地点
		CheckStatus:     in.CheckStatus,     // 盘点状态
		StatusID:        in.StatusID,        // 状态ID
		StatusName:      in.StatusName,
		Attachment:      in.Attachment,                       // 附件
		TenantID:        in.TenantID,                         // 租户ID
		CreatedAtuser:   in.CreatedAtuser,                    // 创建用户
		AssetCategory:   in.AssetCategory,                    //资产分类
		AssetCategoryID: in.AssetCategoryID,                  //资产分类ID
		AssetValue:      in.AssetValue,                       //资产原值
		AssetSource:     in.AssetSource,                      //资产来源
		AssetSourceID:   in.AssetSourceID,                    //资产来源ID
		UsageRoom:       in.UsageRoom,                        //使用科室
		UsageRoomID:     in.UsageRoomID,                      //使用科室ID
		UseDate:         BeanUtils.GetDateFormat(in.UseDate), //使用日期
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
